home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / butnna / clinnav.pas < prev    next >
Pascal/Delphi Source File  |  1995-12-22  |  5KB  |  195 lines

  1. unit Clinnav;
  2.  
  3. (*
  4.    Description
  5.  
  6.    Demostrates the use of the TButtonArray class
  7.  
  8.    This class duplicates DBNavigator except for :
  9.    Uses TButtonArray class for the buttons so that more buttons
  10.    may be added by stretching the control
  11.  
  12.    Instead of using BOF and EOF to grey out buttons it uses a call to
  13.    the database engine so that the BOF is when the cursor is on
  14.    the first record and EOF when the cursor is on the last record.
  15.  
  16.    Usually BOF is one before the first record
  17.    Usually EOF is one after the last record
  18.  
  19.    hint help text and button captions are stored in a RES file CLINNAV.RES
  20.  
  21.    Most of this file is base on DBNavigator
  22.  
  23.    Author Mike Lindre CompuServe USERID 100567,2225
  24.  
  25.    Last Edit 12 July 1995
  26.  *)
  27.  
  28. interface
  29.  
  30. uses
  31.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  32.   Forms, Dialogs, ButArray, DB;
  33.  
  34. type
  35.   ClinButEnum = (enFirst,enPrev,enNext,enLast);
  36.   TClinDataLink = class;
  37.   TClinNav = class(TButtonArray)
  38.   private
  39.     { Private declarations }
  40.     FDataLink:TClinDataLink;
  41.     function  GetDataSource:TDataSource;
  42.     procedure SetDataSource(Value:TDataSource);
  43.     procedure CMEnabledChanged(var Message:TMessage);message CM_ENABLEDCHANGED;
  44.   protected
  45.     { Protected declarations }
  46.     procedure DataChanged;
  47.     procedure ActiveChanged;
  48.     procedure Loaded;override;
  49.     procedure Notification(AComponent:TComponent; Op:TOperation); override;
  50.     procedure ButClick(ButtonIndex:integer);  override;
  51.   public
  52.     { Public declarations }
  53.      constructor Create(AOwner:TComponent);override;
  54.      destructor  Destroy;override;
  55.   published
  56.     { Published declarations }
  57.     property DataSource:TDataSource read GetDataSource write SetDataSource;
  58.   end;
  59.  
  60.   TClinDataLink = Class(TDataLink)
  61.   private
  62.      FClinNav :TClinNav;
  63.   protected
  64.      procedure DataSetChanged;override;
  65.      procedure ActiveChanged; override;
  66.   public
  67.      constructor Create(AClinNav:TClinNav);
  68.      destructor  Destroy;override;
  69.   end;
  70.  
  71. implementation
  72.  
  73. uses DbiTypes, DbiProcs, DbiErrs;
  74. {$R CLINNAV.RES}
  75.  
  76. constructor TClinNav.Create(AOwner:TComponent);
  77. begin
  78.   inherited Create(AOwner);
  79.   {Set the Defaults}
  80.   NoOfButtons  := 4;
  81.   {Set the Resource numbers of the help text and button captions}
  82.   {The text is stored in CLINNAV.RES as two string tables }
  83.   NameResource := 200;
  84.   HintResource := 400;
  85.   {make a datalink}
  86.   FDataLink := TClinDataLink.Create(Self);
  87.   ShowHint := True;
  88. end;
  89.  
  90. destructor TClinNav.Destroy;
  91. begin
  92.   FDataLink.Free;
  93.   FDataLink := nil;
  94.   inherited Destroy;
  95. end;
  96.  
  97. procedure TClinNav.ButClick(ButtonIndex:integer);
  98. {Add the database navigation to the buttons}
  99. begin
  100.   if (DataSource <> nil) and (DataSource.State <> dsInactive) then begin
  101.     with DataSource.DataSet do begin
  102.        case ClinButEnum(ButtonIndex) of
  103.          enFirst:First;
  104.          enNext:Next;
  105.          enPrev:Prior;
  106.          enLast:Last;
  107.        end;
  108.     end;
  109.   end;
  110.   inherited ButClick(ButtonIndex);
  111. end;
  112.  
  113. procedure TClinNav.Notification(AComponent:TComponent; Op:TOperation);
  114. {If the database goes set field to nil}
  115. begin
  116.   inherited Notification(AComponent,Op);
  117.   if (Op = opRemove) and (FDataLink <> nil ) and
  118.      (AComponent = DataSource) then DataSource := nil;
  119. end;
  120.  
  121. procedure TClinNav.SetDataSource(Value:TDataSource);
  122. begin
  123.   FDataLink.DataSource := Value;
  124.   if not (csloading in ComponentState) then ActiveChanged;
  125. end;
  126.  
  127. function TClinNav.GetDataSource: TDataSource;
  128. begin
  129.   Result:=FDataLink.DataSource;
  130. end;
  131.  
  132. procedure TClinNav.DataChanged;
  133. var UpEnabled:Boolean;
  134.     DnEnabled:Boolean;
  135.     Count:longint;
  136. begin
  137.    {Check position in database, by first updating the cursor position}
  138.    FDataLink.DataSet.UpdateCursorPos;
  139.    {Reading the current record position}
  140.    DbiGetSeqNo (FDataLink.DataSet.Handle, Count);
  141.    {Enable or Disable according to the record position}
  142.    UpEnabled := Enabled and FDataLink.Active
  143.       and not ((Count <= 1) or FDataLink.DataSet.BOF);
  144.    DnEnabled := Enabled and FDataLink.Active
  145.       and not ((Count >= FDataLink.DataSet.RecordCount) or FDataLink.DataSet.EOF);
  146.    {button order first prev next last}
  147.    EnableButtons([UpEnabled,UpEnabled,DnEnabled,DnEnabled]);
  148. end;
  149.  
  150. procedure TClinNav.ActiveChanged;
  151. begin
  152.   if not (Enabled and FDataLink.Active) then
  153.     EnableButtons([false,false,false,false])
  154.   else DataChanged;
  155. end;
  156.  
  157. procedure TClinNav.CMEnabledChanged(var Message:TMessage);
  158. begin
  159.   inherited;
  160.   if not (csLoading in ComponentState) then
  161.     ActiveChanged;
  162. end;
  163.  
  164. procedure TClinNav.Loaded;
  165. begin
  166.   inherited Loaded;
  167.   ActiveChanged;
  168. end;
  169.  
  170. {The DataLink}
  171.  
  172. constructor TClinDataLink.Create(AClinNav:TClinNav);
  173. begin
  174.   inherited Create;
  175.   FClinNav := AClinNav;
  176. end;
  177.  
  178. destructor TclinDataLink.Destroy;
  179. begin
  180.   FClinNav := nil;
  181.   inherited Destroy;
  182. end;
  183.  
  184. procedure TClinDataLink.DataSetChanged;
  185. begin
  186.   if FClinNav <> nil then FClinNav.DataChanged;
  187. end;
  188.  
  189. procedure TClinDataLink.ActiveChanged;
  190. begin
  191.   if FClinNav <> nil then FClinNav.ActiveChanged;
  192. end;
  193.  
  194. end.
  195.